package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.phoenixcoin.MainActivity;
import com.sskj.phoenixcoin.R;
import com.sskj.lib.base.BaseActivity;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.LoginActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;


// 登录界面
@Route(path = RConfig.APP_LOGIN)
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R.id.rl_see)
    CheckBox rlSee;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ImmersionBar.with(this)
                .statusBarColor(R.color.app_040)
                .statusBarDarkFont(false)
                .fitsSystemWindows(true)
                .init();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.clear();
//        userViewModel.getUsers().observe(this, users -> {
//            if (users != null && users.size() > 0) {
//                userData = users.get(0);
//            } else {
//                userData = null;
//            }
//        });
        rlSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        ClickUtil.click(300, btnLogin, () -> {
            if (TextUtils.isEmpty(etMobile.getText().toString().trim())) {
                ToastUtil.showShort("请输入您的手机号");
                return;
            }
            if (!CheckUtil.isMobile(etMobile.getText().toString().trim())) {
                ToastUtil.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(etPwd.getText().toString())) {
                ToastUtil.showShort("请输入登录密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etPwd.getText().toString())) {
                ToastUtil.showShort("您的登录密码格式错误");
                return;
            }
            mPresenter.login(etMobile.getText().toString().trim(), etPwd.getText().toString());
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    public void loginSuccess(UserData userData) {
        if (userData != null) {
            SPUtil.put(SPConfig.ID, userData.getUserUid());
            HttpHeaders headers = new HttpHeaders();
            userViewModel.insert(userData);
            headers.put("token", userData.getToken());
            HttpParams httpParams = new HttpParams();
            httpParams.put("systemType", "ANDROID");
            OkGo.getInstance().addCommonHeaders(headers);
            OkGo.getInstance().addCommonParams(httpParams);
        }
        MainActivity.start(this);
        finish();
    }

}
